<?php
 /**
 * 截取部分字符串
 *
 * LICENSE: 
 * @author $Id: Cutword.php 31 2011-11-22 06:22:25Z kylingood $
 * @since      Release 1.0.0
 * @copyright  Copyright (c) 2010-2011 Mumayi.com Inc. (http://www.mumayi.com)
*/

class Custom_View_Helper_Cutword extends Zend_View_Helper_Abstract
{
	/**
	 * 
	 * Enter description here ...
	 * @param string $string  需要截取的字符串参数
	 * @param int $length  需要截取字串长度
	 * @param string $etc  省略字串
	 * @param string $charset  字串编码类型
	 * @param bool $suffix 是否截取
	 */
	public	function cutword($string,$length=80,$etc='...',$charset="utf-8",$suffix = true) {
		$start = 0;
		$string=strip_tags($string);
		if (function_exists ( "mb_substr" )){
			$more = (mb_strlen($string, $charset) > $length) ? true : false;
			return mb_substr ( $string, $start, $length, $charset ).($more?$etc:'');			
		}
		$re ['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
		$re ['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
		$re ['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
		$re ['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
		preg_match_all($re[$charset], $string, $match);
		$slice = join ('', array_slice ($match[0], $start, $length ) );
		if ($suffix) return $slice.$etc;
		return $slice;
	}
	
}